package com.cdks.wuxiu.api.service;


import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.component.aliyun.RealNameAuth;
import com.cdks.wuxiu.api.model.dto.*;
import com.cdks.wuxiu.api.service.mysql.DailyUserInfoService;
import com.cdks.wuxiu.api.service.mysql.UserAlbumInfoService;
import com.cdks.wuxiu.api.service.mysql.UserAuthInfoService;
import com.cdks.wuxiu.api.service.mysql.UserBaseInfoService;
import com.cdks.wuxiu.api.service.mysql.UserBlockInfoService;
import com.cdks.wuxiu.api.service.mysql.UserExtendInfoService;
import com.cdks.wuxiu.api.service.mysql.UserFansInfoService;
import com.cdks.wuxiu.api.service.mysql.UserFriendInfoService;
import com.cdks.wuxiu.api.service.mysql.UserInviteInfoService;
import com.cdks.wuxiu.api.service.mysql.UserLoginInfoService;
import com.cdks.wuxiu.api.service.mysql.UserScoreInfoService;
import com.cdks.wuxiu.api.service.mysql.UserStatisInfoService;
import com.cdks.wuxiu.api.service.mysql.VideoShowInfoService;
import com.cdks.wuxiu.api.service.redis.RedisService;
import com.cdks.wuxiu.api.util.TimeUtils;
import com.cdks.wuxiu.common.exception.ApiException;
import com.cdks.wuxiu.common.util.ServletUtils;
import com.cdks.wuxiu.entity.mysql.UserAuthInfo;
import com.cdks.wuxiu.entity.mysql.UserBaseInfo;
import com.cdks.wuxiu.entity.mysql.UserBlockInfo;
import com.cdks.wuxiu.entity.mysql.UserExtendInfo;
import com.cdks.wuxiu.entity.mysql.UserFansInfo;
import com.cdks.wuxiu.entity.mysql.UserFriendInfo;
import com.cdks.wuxiu.entity.mysql.UserInviteInfo;
import com.cdks.wuxiu.entity.mysql.UserLoginInfo;
import cn.hutool.crypto.SecureUtil;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;


/**
 * 用户服务实现类
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserBaseInfoService userBaseInfoService;
    @Resource
    private UserStatisInfoService userStatisInfoService;
    @Resource
    private UserFansInfoService userFansInfoService;
    @Resource
    private UserAuthInfoService userAuthInfoService;
    @Resource
    private UserInviteInfoService userInviteInfoService;
    @Resource
    private UserLoginInfoService userLoginInfoService;
    @Resource
    private UserBlockInfoService userBlockInfoService;
    @Resource
    private UserAlbumInfoService userAlbumInfoService;
    @Resource
    private DailyUserInfoService dailyUserInfoService;
    @Resource
    private UserExtendInfoService userExtendInfoService;
    @Resource
    private UserFriendInfoService userFriendsInfoService;
    @Resource
    private RedisService redisService;
    

    @Override
    public UserBaseInfo getUserById(long userId) {
        return userBaseInfoService.getInfoById(userId);
    }

    @Override
    public UserBaseInfo getUserByPhone(String phone) {
        return userBaseInfoService.getInfoByPhone(phone);
    }

    @Override
    public Long getUserByIdcard(String idCard) {
        return userAuthInfoService.getUserByIdCard(idCard);
    }

    @Override
    public UserInfoDTO getUserByIdWithFocus(long userId, long fansId) {
        return userBaseInfoService.getInfoByIdWithFocus(userId, fansId);
    }

    @Override
    public UserInfoDTO getUserByIdWithBalance(long userId, long expire) {
        return userBaseInfoService.getInfoByIdWithBalance(userId, expire);
    }

    @Override
    public int autoInviter(UserBaseInfo userInfo) {
        if (userInfo == null)
        {
            throw new ApiException(10, "用户信息有误");
        }

        //根据ip地址获取邀请信息
        String inviter  = redisService.getInviterInfo(ServletUtils.getIpAddress());
        if (inviter == null)
        {
            return -1;
        }

        //保存邀请信息
        long inviterId  = Long.parseLong(inviter);
        UserInviteInfo  info = new UserInviteInfo();
        info.setCreateTime(TimeUtils.getTimeSecond());
        info.setInviter(inviterId);
        info.setUserId(userInfo.getUserId());
        userInviteInfoService.save(info);

        //更新邀请人
        int ret  = userBaseInfoService.autoInviter(userInfo, inviterId);
        if (ret < 0)
        {
            return -2;
        }

        return 0;
    }

    @Override
    public int bindInviter(UserBaseInfo userInfo, long inviter) {
        if (userInfo == null)
        {
            throw new ApiException(10, "用户信息有误");
        }

        int ret  = userBaseInfoService.bindInviter(userInfo, inviter);
        if (ret < 0)
        {
            return -1;
        }

        return 0;
    }

    @Override
    public int updateInviter(UserBaseInfo userInfo, long inviter) {
        if (userInfo == null)
        {
            throw new ApiException(10, "用户信息有误");
        }

        if (userInfo.getBindInviter() > 0)
        {
            throw new ApiException(10, "已绑定邀请人，如需修改请联系客服");
        }

        int ret  = userBaseInfoService.updateInviter(userInfo, inviter);
        if (ret < 0)
        {
            return -1;
        }

        return 0;
    }

    @Override
    public int setInviteStatus(UserBaseInfo userInfo, int status) {
        int ret  = userBaseInfoService.setInviteStatus(userInfo, status);
        if (ret < 0)
        {
            return -1;
        }
        
        return 0;
    }

    @Override
    public int setFocus(Long userId, Long destId, Integer action) {
        int ret;

        if (action == Constant.USER_ACTION_CANCEL)
        {
            // 取消关注
            ret  = userFansInfoService.cancel(userId, destId);
            if (ret < 0)
            {
                return -1;
            }

            if (ret == 1)
            {
                return 0;
            }

            // 更新用户粉丝数
            ret  = userStatisInfoService.decFansCount(destId);
            if (ret < 0)
            {
                return -2;
            }
        }
        else
        {
            // 关注
            ret  = userFansInfoService.focus(userId, destId);
            if (ret < 0)
            {
                return -1;
            }

            if (ret == 1)
            {
                return 0;
            }

            // 更新用户粉丝数
            ret  = userStatisInfoService.addFansCount(destId);
            if (ret < 0)
            {
                return -2;
            }
        }

        return 0;
    }

    @Override
    public UserFansInfo queryFocusByUser(long userId, long destId) {
        return userFansInfoService.queryFocusByUser(userId, destId);
    }

    @Override
    public List<UserFocusDTO> getFocusList(Long userId, Long startId) {
        //安卓小于1.0.2(3)、iOS小于1.0.9(9)版本没有 startId 参数
        return userFansInfoService.getList(userId, (startId == null) ? 0 : startId);
    }

    @Override
    public int cancelFocus(long userId, long destId) {
        int ret;


        // 取消关注
        ret  = userFansInfoService.cancel(userId, destId);
        if (ret < 0)
        {
            return -1;
        }

        if (ret == 1)
        {
            return 0;
        }

        // 更新用户粉丝数
        ret  = userStatisInfoService.decFansCount(destId);
        if (ret < 0)
        {
            return -2;
        }

        return 0;
    }

    @Override
    public int attachFocus(long userId, long destId) {
        int ret;


        // 关注
        ret  = userFansInfoService.focus(userId, destId);
        if (ret < 0)
        {
            return -1;
        }

        if (ret == 1)
        {
            return 0;
        }

        // 更新用户粉丝数
        ret  = userStatisInfoService.addFansCount(destId);
        if (ret < 0)
        {
            return -2;
        }

        return 0;
    }

    @Override
    public int cancelBlock(long userId, long destId) {
        int ret;

        // 取消屏蔽
        ret  = userBlockInfoService.cancel(userId, destId);
        if (ret < 0)
        {
            return -1;
        }

        return 0;
    }

    @Override
    public int attachBlock(long userId, long destId) {
        int ret;


        // 屏蔽
        ret  = userBlockInfoService.block(userId, destId);
        if (ret < 0)
        {
            return -1;
        }

        cancelFocus(userId, destId);

        return 0;
    }

    @Override
    public int saveAuditInfo(int auditType, long userId, String info, int status) {
        return saveAuditInfo(auditType, userId, info, status, null);
    }

    @Override
    public int saveAuditInfo(int auditType, long userId, String info, int status, String remark) {
        UserAuthInfo audit  = new UserAuthInfo();
        audit.setCreateTime(TimeUtils.getTimeSecond());
        audit.setInfo(info);
        audit.setType(auditType);
        audit.setUserId(userId);
        audit.setStatus(status);
        audit.setRemark(remark);

        userAuthInfoService.save(audit);

        return 0;
    }

    @Override
    public String getAuditNickname(long userId) {
        return userAuthInfoService.getNickname(userId);
    }

    @Override
    public String getAuditPortrait(long userId) {
        return userAuthInfoService.getPortrait(userId);
    }

    @Override
    public int saveUserBaseInfo(UserBaseInfo userInfo) {
        userBaseInfoService.updateById(userInfo);

        return 0;
    }

    @Override
    public int saveUserExtendInfo(UserExtendInfo userInfo) {
        userExtendInfoService.saveInfo(userInfo);

        return 0;
    }

    @Override
    public int realNameAuth(String name, String idCard, UserBaseInfo userInfo) {
        int ret  = RealNameAuth.checkInfo(idCard, userInfo.getPhone(), name);
        if (ret < 0)
        {
            return ret;
        }

        userInfo.setSex(ret);
        userInfo.setAuthStatus(userInfo.getAuthStatus() | Constant.AUTH_TYPE_REALNAME);

        saveAuditInfo(Constant.AUDIT_TYPE_REALNAME, userInfo.getUserId(), SecureUtil.md5(idCard), Constant.AUDIT_STATUS_PASS, name + "," + idCard);

        return 0;
    }

    @Override
    public int getAuthStatus(UserBaseInfo userInfo) {
        if ((userInfo.getInfoStatus() & Constant.INFO_STATUS_AUTH) > 0)
        {
            return Constant.AUTH_STATUS_WAIT;
        }

        if (userInfo.getAuthRole() > 0)
        {
            return Constant.AUTH_STATUS_PASS;
        }

        return Constant.AUTH_STATUS_NONE;
    }

    @Override
    public List<UserListDTO> getUserList(long self, int sex, int role, int userType, int type, int page) {
        return userBaseInfoService.getList(self, sex, role, userType, type, page);
    }

    @Override
    public List<UserBaseInfo> getRandCallList(long userId, int sex) {
        return userBaseInfoService.getRandCallList(userId, sex);
    }

    @Override
    public List<UserBaseInfo> getKefuList() {
        return userBaseInfoService.getKefuList();
    }

    @Override
    public List<UserInfoDTO> getInviteList(long userId, int page, int start, int stop) {
        return userInviteInfoService.getList(userId, page, start, stop);
    }

    @Override
    public int updatePushInfo(long userId, String token) {
        int ret  = userLoginInfoService.updatePushToken(userId, token);
        if (ret < 0)
        {
            return -1;
        }

        return 0;
    }

    @Override
    public int updatePushInfo(long userId, String token, int status) {
        int ret  = userLoginInfoService.updatePushToken(userId, token, status);
        if (ret < 0)
        {
            return -1;
        }

        return 0;
    }

    @Override
    public InviteStatisDTO getInviteStatis(long userId) {
        return userInviteInfoService.getStatisByUser(userId);
    }

    @Override
    public UserBaseInfo getInviterInfo(long userId) {
        return userBaseInfoService.getInviterInfo(userId);
    }

    @Override
    public UserExtendInfo getUserExtendInfo(long userId) {
        return userExtendInfoService.getInfoByUserId(userId);
    }

    @Override
    public int refuseHeadAudit(long userId) {
        return userAlbumInfoService.refusePortrait(userId);
    }

    @Override
    public int setUserIdleTime(UserBaseInfo userInfo) {
        //未认证用户不更新最后空闲时间
        if (userInfo.getAuthRole() > 0)
        {
            dailyUserInfoService.setIdleTime(userInfo.getUserId());
        }

        return 0;
    }

    @Override
    public int setUserIdleTime(long userId) {
        dailyUserInfoService.setIdleTime(userId);

        return 0;
    }

    @Override
    public UserLoginInfo getUserLoginInfo(long userId) {
        return userLoginInfoService.getByUserId(userId);
    }

    @Override
    public boolean isBlocked(long userId, long destId) {
        UserBlockInfo info  = userBlockInfoService.getBlockedInfo(userId, destId);
        if (info == null)
        {
            return false;
        }

        return true;
    }

    @Override
    public int setUserOnlineStatus(long userId, int status) {
        return userBaseInfoService.updateOnlineStatus(userId, status);
    }

    @Override
    public int setUserOnlineStatus(long userId, int oldStatus, int newStatus) {
        return userBaseInfoService.updateOnlineStatus(userId, oldStatus, newStatus);
    }

    @Override
    public int setUserOnlineStatus(Set<String> ids, int oldStatus, int newStatus) {
        return userBaseInfoService.updateOnlineStatus(ids, oldStatus, newStatus);
    }

    @Override
    public UserAuthInfo getAuthInfoByType(int type, long userId) {
        return userAuthInfoService.getInfoByType(type, userId);
    }

    @Override
    public List<UserAuthInfo> getAuthInfoByStatus(int status, long userId) {
        return userAuthInfoService.getListByStatus(status, userId);
    }

    @Override
    public int updateListenerInfo(UserBaseInfo userInfo) {
        if (userInfo.getAuthRole() == Constant.AUTH_ROLE_TALKER)
        {
            return 0;
        }

        UserAnswerStatisDTO statis  = dailyUserInfoService.getAnswerStatis(userInfo.getUserId());
        //更新被叫接通率和认证等级
        userBaseInfoService.updateListenerInfo(userInfo, statis);

        return 0;
    }

    @Override
    public int cancelUserAuth(long userId) {
        return userBaseInfoService.cancelAuth(userId);
    }

    @Override
    public int setFriend(long userId, long friendId, int action) {
        switch (action)
        {
            case Constant.USER_ACTION_REQUEST:
            {
                userFriendsInfoService.request(userId, friendId);
                
                break;
            }     
            case Constant.USER_ACTION_AGREE:
            {
                userFriendsInfoService.agree(userId, friendId);
                
                break;
            }
            case Constant.USER_ACTION_CANCEL:
            case Constant.USER_ACTION_REFUSE:
            {
                userFriendsInfoService.delete(userId, friendId);
                
                break;
            }
        }

        return 0;
    }

    @Override
    public UserFriendInfo queryFriend(long userId, long friendId) {
        return userFriendsInfoService.queryByIdentity(userId, friendId);
    }

    @Override
    public List<UserFriendDTO> getFriendList(long userId, long startId) {
        return userFriendsInfoService.getList(userId, startId);
    }
}
